package hz.mobileproject.pda.presenter;

import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.request.Request_login_user;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.entity.response.Response_loadPrisonerList;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.activity.LoginActivity;
import hz.mobileproject.pda.utils.Base64;
import hz.mobileproject.pda.utils.MacUtils;
import hz.mobileproject.pda.utils.NetWorkUtils;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class PLogin_data extends XPresent<LoginActivity>{

    private Gson gson;
    public void loadData(String policeCode,  String password) {
        gson = new Gson();
        Request_login_user request_login_user= new Request_login_user();
        request_login_user.setPoliceCode(policeCode);
        request_login_user.setPassword(password);
        request_login_user.setMacAddress(MacUtils.getMacAddr());
        String login_user=  gson.toJson(request_login_user);

        Request_data    data = new Request_data();
        data.setData(Base64.getBase64(login_user));
        String  body_data=  gson.toJson(data);

        RequestBody bodsy=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),body_data);


       /* Api.getGankService().getloginData(bodsy)
                .compose(XApi.<Response_data_login>getApiTransformer())
                .compose(XApi.<Response_data_login>getScheduler())
                .compose(getV().<Response_data_login>bindToLifecycle())
                .subscribe(new ApiSubscriber<Response_data_login>() {
                    @Override
                    protected void onFail(NetError error) {
                        LogUtils.e("onFail");
                        getV().showError(error);
                }

                    @Override
                    public void onNext(Response_data_login response_data) {
                        LogUtils.e("onNext");
                        LogUtils.e("PLogin"+Base64.getFromBase64(response_data.getData()));
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        LogUtils.e("onComplete");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LogUtils.e("onError");
                    }

                    @Override
                    protected void onStart() {
                        super.onStart();
                        LogUtils.e("onStart");
                    }

                });*/


        RequestParams params = new RequestParams(Constance.BASE_URL + Constance.URL_LOGIN);
        LogUtils.e("login",params);

        params.setAsJsonContent(true);
        params.setBodyContent(body_data.toString());


        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                LogUtils.e(result);

                Response_data response_data= gson.fromJson(result,Response_data.class);

                if (response_data.getRtnCode().equals("1")){
                    Toast.makeText(x.app(), "请检查密码", Toast.LENGTH_LONG).show();
                }else {
                    getV().showData(Base64.getFromBase64(response_data.getData()));
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                if (NetWorkUtils.isNetworkConnected(getV().getApplicationContext())){
//                    Toast.makeText(x.app(), "后台服务已停止,请联系技术人员", Toast.LENGTH_LONG).show();
                    Toast.makeText(x.app(), "连接服务器失败", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(x.app(), "网络未连接，请连接网络", Toast.LENGTH_LONG).show();
                }
                getV().showError(ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void loadData(String login_user) {
        Request_data    data = new Request_data();
        data.setData(Base64.getBase64(login_user));
        String  body_data=  gson.toJson(data);

        RequestBody bodsy=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),body_data);

        Api.getGankService().getloadPrisonerList(bodsy,"")
                .compose(XApi.<Response_data>getApiTransformer())
                .compose(XApi.<Response_data>getScheduler())
                .compose(getV().<Response_data>bindToLifecycle())
                .subscribe(new ApiSubscriber<Response_data>() {
                    @Override
                    protected void onFail(NetError error) {
//                        getV().showError(error);
                        getV().skip();
                    }

                    @Override
                    public void onNext(Response_data data) {
//                                getV().respnose_data(gson.toJson(data),"分监区查询");
                        Base64.getFromBase64(data.getData());
                        LogUtils.e("根据部门编号获取需要点名的罪犯列表"+Base64.getFromBase64(data.getData()));

                        if (data.getData().equals("W10=")){
                            getV().skip();
                        }else {

                            String data_json= Base64.getFromBase64(data.getData());
                            List<Response_loadPrisonerList>    prisonersBeanList = gson.fromJson(data_json, new TypeToken<List<Response_loadPrisonerList>>() {}.getType());
                            getV().save_data(prisonersBeanList);

                        }
                    }

                });
    }


}
